package de.breitbeil.jbooking.ui.dialog;

import javax.sql.DataSource;
import javax.swing.JComponent;

import org.springframework.richclient.dialog.FormBackedDialogPage;
import org.springframework.richclient.dialog.TitledApplicationDialog;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;
import org.springframework.richclient.form.Form;
import org.springframework.richclient.settings.SettingsException;
import org.springframework.richclient.settings.SettingsManager;

import de.breitbeil.jbooking.task.BookingTemplateTask;
import de.breitbeil.jbooking.task.DerbyExportTask;
import de.breitbeil.jbooking.ui.form.BookingTemplateForm;
import de.breitbeil.jbooking.ui.form.BookingTemplateFormObject;
import de.breitbeil.jbooking.ui.form.ExportForm;
import de.breitbeil.jbooking.ui.form.ExportFormObject;

public class ExportDialog extends TitledPageApplicationDialog {

    public static final String ID = "exportDialog"; 

    private Form form;
    private DataSource dataSource;

    public ExportDialog(SettingsManager settingsManager, DataSource dataSource) throws SettingsException {
        this.dataSource = dataSource;
        
        ExportFormObject formObject = new ExportFormObject();
        formObject.init(settingsManager.getUserSettings());
        
        form = new ExportForm(formObject);
        
        setDialogPage(new FormBackedDialogPage(form));
    }
    
    protected void onInitialized() {
        super.onInitialized();

        setTitle(getMessage(ID + ".title"));
        setTitlePaneTitle(getMessage(ID + ".title"));
        setDescription(getMessage(ID + ".description"));

        setResizable(false);
    }
    
    @Override
    protected boolean onFinish() {
        form.commit();
        ExportFormObject formObject = (ExportFormObject) form.getFormObject();
        
        // Save database to file
        DerbyExportTask task = new DerbyExportTask(formObject, dataSource);
        task.execute();
        
        return true;
    }

}
